package lib.bone.admin.event;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import lib.bone.flow.constant.FlowTaskStatusConst;
import lib.bone.flow.constant.FlowWorkStatusConst;
import lib.bone.flow.domain.entity.FlowTask;
import lib.bone.flow.domain.entity.FlowTaskAssign;
import lib.bone.flow.domain.entity.FlowWork;
import lib.bone.flow.service.FlowTaskAssignService;
import lib.bone.flow.service.FlowTaskService;
import lib.bone.flow.service.FlowWorkService;
import lib.bone.system.event.AuthRoleEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 角色授权事件接口实现
 */
@Slf4j
@Service
public class AuthRoleEventImpl implements AuthRoleEvent {

    @Autowired
    FlowTaskAssignService flowTaskAssignService;

    @Autowired
    FlowWorkService flowWorkService;

    @Autowired
    FlowTaskService flowTaskService;

//    @Autowired(required = false)
//    FlowTaskCreatedEvent flowTaskCreatedEvent;

    @Override
    public void process(Long roleId, Long userId) {

        /**
         * 1. 根据角色ID 查询流程审核指派 根据 assignType 和 assignId = roleId
         * 2. 根据流程审核指派获取 flowDefineCode 和 flowNodeId 对应的是work 表中的 flow_define_code 和 current_node_id
         * 3. 根据 3 个条件 flow_define_code 和 current_node_id 和 flow_work_status == 进行中 查询flowWork 列表
         * 4. 根据查询到的flowWork列表 创建该角色对应的 task
         */
        Optional<FlowTaskAssign> flowTaskAssignOp = flowTaskAssignService.getByAssignId(roleId.toString());
        FlowTaskAssign flowTaskAssign = flowTaskAssignOp.get();
        String flowDefineCode=flowTaskAssign.getFlowDefineCode();
        String flowNodeId=flowTaskAssign.getFlowNodeId();
        String  flowWorkStatus = FlowWorkStatusConst.IN_PROGRESS;

        List<FlowWork> flowWorkList =flowWorkService.getFlowWorks(flowDefineCode,flowNodeId,flowWorkStatus);
        for (FlowWork flowWork:flowWorkList){
            FlowTask flowTask=new FlowTask();
            flowTask.setActUserId(userId.toString());
            flowTask.setFlowWorkId(flowWork.getId());
            flowTask.setFlowNodeId(flowWork.getCurrentNodeId());
            Optional<FlowTask> flowTaskOp=flowTaskService.findByFlowWorkId(flowWork.getId());
            FlowTask task=flowTaskOp.get();
            flowTask.setFlowParam(task.getFlowParam());
            flowTask.setTaskName(task.getTaskName());
            flowTask.setOperator(task.getOperator());
            flowTask.setInitiator(flowWork.getInitiator());
            flowTask.setTaskStatus(FlowTaskStatusConst.CREATED);
            flowTask.setServiceType(task.getServiceType());
            flowTask.setServiceId(task.getServiceId());
            flowTask.setServiceName(task.getServiceName());
            flowTask.setStatus(1);
            BeanUtil.copyProperties(flowTask, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            flowTaskService.saveOne(flowTask);
        }

    }
}
